<?php
namespace ChannelThrift\Service;

use Base\BaseService;

class CarService extends BaseService {
    Protected $autoCheckFields = false;
    public  $connection = 'DB_AUMS';
    public $tablePrefix = 'au_';


    /**
     * 获取报告基本信息
     * @param integer $carId
     * @return array
     */
    public function getCar($carId)
    {
        if (empty($carId)) {
            return false;
        }
        return D('ChannelThrift/Aums/Car')->where('car_id = ' . $carId)->find();
    }

    /**
     * 更新车源
     * @param $carId int 报告ID
     * @param $data array 更新数据
     * @return Boolean
     * */
    public function update($carId,$data)
    {
        $map = array('car_id'=>array('eq',$carId));
        $result = D('ChannelThrift/Aums/Car')->where($map)->save($data);
        return $result;
    }

    /**
     * 添加记录车辆跟踪信息
     * @param int $car_id
     * @param array $car_action_data
     * @return int
     */
    public static function addCarTraceLog($carId, $carActionData)
    {
        $m = D('ChannelThrift/Aums/Car');
        $carInfo = $m->where(['car_id' => $carId])->find();
        $sid = $carInfo['sid'];

        $carTrace = D('ChannelThrift/Aums/CarTraceLogList');
        $owner_id = $m->where(['car_id' => $carId])->getField('owner_id');
        $carTraceLog = array();
        $carTraceLog['owner_id'] = $owner_id;
        $carTraceLog['car_id'] = $carId;
        $emp_id = $carActionData['emp_id'];
        $employee = EmployeeService::user_info_id($emp_id);
        if (!empty($employee)) {
            $emp_name = $employee['real_name'];
        } else {
            $emp_name = '--';
        }
        $carTraceLog['sid'] = $sid;
        $carTraceLog['emp_id'] = $emp_id;
        $carTraceLog['emp_name'] = $emp_name;
        $carTraceLog['action_no'] = array_keys(C('CAR_ACTION_NAME'), $carActionData['action_name'])[0];
        $carTraceLog['action_name'] = $carActionData['action_name'];
        $carTraceLog['createtime'] = microtime_time();
        $ok = $carTrace->add($carTraceLog);
        return $ok;
    }


}
